#include<cstdio>
#include<cstring>
const int N=200010;
const int INF=0x3f3f3f3f;
int f[2][N],w[101],v[101],n;
int main()
{
	while(scanf("%d",&n)!=EOF)
	{
		int max=0,min=0;
		for(int i=1;i<=n;i++)
		{
			scanf("%d%d",&w[i],&v[i]);
			if(w[i]>0)max+=w[i];
			else min+=(-w[i]);
		}
		for(int i=0;i<N;i++)f[1][i]=-0x3f3f3f3f;
		f[1][w[1]+100000]=v[1];f[1][100000]=0;
		for(int i=2;i<=n;i++)
			for(int j=max+100000;j>=-min+100000;j--)
			{
				f[i%2][j]=-INF;
				if(j-100000==0)f[i%2][j]=0;
				 if(f[(i-1)%2][j]!=-INF)f[i%2][j]=f[(i-1)%2][j];
				if(j-w[i]-100000>=-min&&j-w[i]-100000<=max)
				{
				   if(f[(i-1)%2][j-w[i]]!=-INF&&f[(i-1)%2][j-w[i]]+v[i]>f[i%2][j])
					  f[i%2][j]=f[(i-1)%2][j-w[i]]+v[i];
				}
			}
		int ans=0,tt;
		for(int i=100000;i<=max+100000;i++)
			if(f[n%2][i]>=0&&f[n%2][i]+i-100000>ans){ans=f[n%2][i]+i-100000;tt=i;}
		printf("%d\n",ans);
	}
	return 0;
}

